<?php

namespace app\purchase\model;

use think\Db;

class Cart extends \ShopModel
{
    protected $table = 'sh_cart';

//    protected $pk = 'cart_id';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'create_at';

    // 一条记录对应一个商品

    public function goods()
    {
        return $this->hasOne('Goods', 'goods_id', 'goods_id');
    }
    public function cartDb(){
        return Db::name('cart');
    }
    public function getGoodsInfo($client_id){
        $cartData = $this->where('client_id',$client_id)->select();
        $retData = [];
        $Goods = new Goods();
        foreach ($cartData as $k=>$v){
            $data = $Goods->gIdAndUnitPrice($v['goods_id'],$v['unit_id'],$client_id);
            $data['quantity'] = $v['quantity'];
            $data['subtotal'] = $v['quantity']*$data['price'];
            $retData[] = $data;
        }
        return $retData;
    }
}
